/**
 * Created by admin on 14-7-23.
 */
if(typeof (module) !="undefined"){
    module.exports = TextTableTd;
    var Serializer = require('./Serializer');
    var Parser = require('./Parser');
    var PointPath = require('../common/PointPath');
    var Point = require('../common/Point');
    var Effect = require('../common/Effect');
    var PrintObj = require('./PrintObj');
}


function TextTableTd() {
    this.innerId = 0;
    this.left = 0;
    this.right = 0;
    this.top = 0;
    this.bottom = 0;
    this.leftEf = new Array();// 背景存放在leftEffect中
    this.rightEf = new Array();	//effect array,特效
    this.topEf = new Array();	//effect array,特效
    this.bottomEf = new Array();	//effect array,特效
    this.sub = new Array();// undo时只保存了 children的id，在保存到服务器时是子的内容
    this.w = 0;// 宽
    this.h = 0;// 高
    this.x = 0;
    this.y = 0;
    this.obliqueDownLine = false;// 斜下划线
    this.obliqueUpLine = false;// 斜上划线
}

// 从json对象中解析出PointPath
TextTableTd.prototype.fromJson = function (jsonObj, versionId) {

    this.innerId = jsonObj.innerId;
    this.left = jsonObj.left;
    this.right = jsonObj.right;
    this.top = jsonObj.top;
    this.bottom = jsonObj.bottom;
    if (jsonObj.hasOwnProperty("leftEf")) {
        this.leftEf.splice(0, this.leftEf.length);

        for (var i = 0; i < jsonObj.leftEf.length; i++) {
            var effect = new Effect();
            effect.fromJson(jsonObj.leftEf[i], versionId);
            this.leftEf.push(effect);
        }
    }
    if (jsonObj.hasOwnProperty("rightEf")) {
        this.rightEf.splice(0, this.rightEf.length);

        for (var i = 0; i < jsonObj.rightEf.length; i++) {
            var effect = new Effect();
            effect.fromJson(jsonObj.rightEf[i], versionId);
            this.rightEf.push(effect);
        }
    }
    if (jsonObj.hasOwnProperty("topEf")) {
        this.topEf.splice(0, this.topEf.length);

        for (var i = 0; i < jsonObj.topEf.length; i++) {
            var effect = new Effect();
            effect.fromJson(jsonObj.topEf[i], versionId);
            this.topEf.push(effect);
        }
    }
    if (jsonObj.hasOwnProperty("bottomEf")) {
        this.bottomEf.splice(0, this.bottomEf.length);

        for (var i = 0; i < jsonObj.bottomEf.length; i++) {
            var effect = new Effect();
            effect.fromJson(jsonObj.bottomEf[i], versionId);
            this.bottomEf.push(effect);
        }
    }

    this.w = jsonObj.w;
    this.h = jsonObj.h;
    this.x = jsonObj.x;
    this.y = jsonObj.y;
    this.obliqueDownLine = jsonObj.obliqueDownLine;
    this.obliqueUpLine = jsonObj.obliqueUpLine;

    if (jsonObj.hasOwnProperty('sub')) {
        for (var i = 0; i < jsonObj.sub.length; i++) {
            var obj = Parser.prototype.fromUnknownJson.call(this, jsonObj.sub[i], versionId);
            this.sub.push(obj);
        }
    }
}
/**
 * 将对象添加到PDF中
 * @param doc  PDFDocument结构
 * @param info  对象结构
 */
TextTableTd.prototype.addToPdf = function (doc, info) {
    // 保存上下文环境
    Serializer.prototype.before.call(this, doc, info);

    // 描绘子对象
    for (var i = 0; i < this.sub.length; i++) {
        this.sub[i].addToPdf(doc, info);
    }
    //描绘边框
    this.drawBorder(doc, info, "left");
    this.drawBorder(doc, info, "right");
    this.drawBorder(doc, info, "top");
    this.drawBorder(doc, info, "bottom");
    //描绘单元格斜线
    this.drawObliqueLine(doc, info);
    //填充单元格
    this.fillTextTableTd(doc,info);
    // 还原上下文环境
    Serializer.prototype.after.call(this, doc, info);
}

/**
 * 描绘边界
 * @param doc  PDFDocument结构
 * @param info  对象结构
 * @param type  边界类型，为："left","right","top","bottom"
 */
TextTableTd.prototype.drawBorder = function (doc, info, type) {
    var ef = ("left".localeCompare(type) == 0) ? this.leftEf : (
        ("right".localeCompare(type) == 0) ? this.rightEf : (
            ("top".localeCompare(type) == 0) ? this.topEf : this.bottomEf
            )
        );
    var clr = Effect.prototype.getEffectFromArray(ef, Effect.prototype.F_EFFECT_TYPE_STROKE, false);
    var width = Effect.prototype.getEffectFromArray(ef, Effect.prototype.F_EFFECT_TYPE_STROKE, true);
    if ((Effect.prototype.F_EFFECT_TYPE_NONE = clr)
        && (Effect.prototype.F_EFFECT_TYPE_NONE = width)) {
        var path = new PointPath();
        //线
        switch (type) {
            case "left":
                //描绘左边框
                var pt0 = new Point();
                pt0.x = 0;
                pt0.y = 0-width/2;
                path.points.push(pt0);
                var pt1 = new Point();
                pt1.x =0;
                pt1.y = this.h+width/2;
                path.points.push(pt1);
                break;
            case "right":
                //描绘右边框
                var pt2 = new Point();
                pt2.x = this.w;
                pt2.y = 0-width/2;
                path.points.push(pt2);
                var pt3 = new Point();
                pt3.x = this.w;
                pt3.y = this.h+width/2;
                path.points.push(pt3);
                break;
            case "top":
                //描绘上边框
                pt0 = new Point();
                pt0.x = 0;
                pt0.y = 0;
                path.points.push(pt0);
                pt2 = new Point();
                pt2.x = this.w;
                pt2.y = 0;
                path.points.push(pt2);
                break;
            case "bottom":
                //描绘底部边框
                pt1 = new Point();
                pt1.x =0;
                pt1.y = this.h;
                path.points.push(pt1);
                pt3 = new Point();
                pt3.x = this.w;
                pt3.y = this.h;
                path.points.push(pt3);
                break;
        }

        var svgPath = path.toSvgPath();
        Serializer.prototype.drawSvgPath.call(this, doc, null, clr,
            width,null,null,
            null,this.al, svgPath);

    }
}
/**
 * 描绘单元格的上，下划斜线
 * @param doc  PDFDocument结构
 * @param info  对象结构
 */
TextTableTd.prototype.drawObliqueLine = function (doc, info) {
    var path = new PointPath();
    if (this.obliqueDownLine == true) {
        var pt0 = new Point();
        pt0.x = 0;
        pt0.y = 0;
        path.points.push(pt0);
        var pt1 = new Point();
        pt1.x = this.w;
        pt1.y = this.h;
        path.points.push(pt1);
        var svgPath = path.toSvgPath();
        doc.path(svgPath).stroke();

    }
    if (this.obliqueUpLine == true) {
        var pt0 = new Point();
        pt0.x = this.w;
        pt0.y = 0;
        path.points.push(pt0);
        var pt1 = new Point();
        pt1.x = 0;
        pt1.y = this.h;
        path.points.push(pt1);
        var svgPath = path.toSvgPath();
        doc.path(svgPath).stroke();
    }
}
/**
 * 单元格填充
 * @param doc  PDFDocument结构
 * @param info  对象结构
 */
TextTableTd.prototype.fillTextTableTd = function(doc,info){
    var ef= this.leftEf;
    var path = new PointPath();
    var fill = Effect.prototype.getEffectFromArray(ef, Effect.prototype.F_EFFECT_TYPE_FILL, false);
    var pt0 = new Point();
    pt0.x = 0;
    pt0.y = 0;
    path.points.push(pt0);
    var pt1 = new Point();
    pt1.x = this.w;
    pt1.y = 0;
    path.points.push(pt1);
    var pt2 = new Point();
    pt2.x = this.w;
    pt2.y = this.h;
    path.points.push(pt2);
    var pt3 = new Point();
    pt3.x = 0;
    pt3.y = this.h;
    path.points.push(pt3);
    var svgPath = path.toSvgPath();
    Serializer.prototype.drawSvgPath.call(this, doc, fill, null,
        null,null,null,
        null,this.al, svgPath);
}
